package com.ruoyi.web.util;


import com.alibaba.fastjson.JSONObject;
import com.apex.livebos.ws.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.xml.namespace.QName;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Component
public class Client {
    private LBEBusinessService serviePort;
    private String sessionId;
    private static final QName SERVICE_NAME = new QName("http://ws.livebos.apex.com/", "LBEBusinessWebService");

    private  static String wsdlUrl;

    private static String userName;

    private static String passWord = "000000";

    @Value("${gssx.wsdlUrl}")
    public void setWsdlUrl(String wsdlUrl) {
        Client.wsdlUrl = wsdlUrl;
    }
    @Value("${gssx.userName}")
    public void setUserName(String userName) {
        Client.userName = userName;
    }
//    @Value("${gssx.passWord}")
//    public void setPassWord(String passWord) {
//        Client.passWord = passWord;
//    }


    public static void main(String[] args) {
        Client client = new Client();
//        client.testBizProcess();
        client.cxSCJDGWDJSJ();
//        client.queryUserKhxx();
    }



    public LBEBusinessService getServiePort() {
        if (serviePort == null) {
            LBEBusinessWebService service;
            URL wsdlURL = null;
            try {
                wsdlURL = new URL(wsdlUrl);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            service = new LBEBusinessWebService(wsdlURL, SERVICE_NAME);
            serviePort = service.getLBEBusinessServiceImplPort();
        }
        return serviePort;
    }

    /**
     * 业务量数据查询
     */
    public  JSONObject cxSCJDGWDJSJ() {
        LBEBusinessService client = getServiePort();
        List<LbParameter> params = new ArrayList<LbParameter>();//入参数组
        String objName = "cxSCJDGWDJSJ";//要调用的接口对象
        QueryResult qrs = client.query(getSessionId(), objName, params, null, new QueryOption());
        JSONObject jsonObject = new JSONObject();
        List<String> keys = new ArrayList<>();
        for (Iterator<ColInfo> it = qrs.getMetaData().getColInfo().iterator(); it.hasNext();) {
            ColInfo colInfo = it.next();
            System.out.print(colInfo.getLabel() + "[" + colInfo.getName() + "]\t");
            keys.add(colInfo.getLabel());
        }
        int n = 0;
        for (Iterator<LbRecord> it = qrs.getRecords().iterator(); it.hasNext();) {
            LbRecord record = it.next();
            for (Iterator<Object> itVal = record.getValues().iterator(); itVal.hasNext();n++) {
                jsonObject.put(keys.get(n),itVal.next());
            }
        }
        return jsonObject;
    }


    /**
     * 2.1.4查询用户密保问题
     */
    public void query() {
        LBEBusinessService client = getServiePort();
        List<LbParameter> params = new ArrayList<LbParameter>();//入参数组
        LbParameter lbParameter = new LbParameter();//入参
        lbParameter.setName("USERID");//入参名称
        lbParameter.setValue("10788");//入参值
        params.add(lbParameter);
        String objName = "cx_KHMBWT";//要调用的接口对象
        QueryResult qrs = client.query(getSessionId(), objName, params, null, new QueryOption());
        printQueryResult(qrs);
    }

    /**
     * 查询客户信息
     */
    public void queryUserKhxx() {
        LBEBusinessService client = getServiePort();
        QueryOption queryOption = new QueryOption();
        queryOption.setBatchNo(1);//当前页
        queryOption.setBatchSize(1);//当前页记录数
        queryOption.setQueryCount(true);//查询总数

        List<LbParameter> params = new ArrayList<LbParameter>();//入参数组
        LbParameter lbParameter = new LbParameter();//入参
        lbParameter.setName("PUSERID");//入参名称
        lbParameter.setValue("10788");//入参值
        params.add(lbParameter);

        String objName = "vHYZL";//要调用的接口对象

        QueryResult qrs = client.query(getSessionId(), objName, params, null, queryOption);
        printQueryResult(qrs);
    }

    protected void printQueryResult(QueryResult qrs) {
        System.out.println("result message: " + qrs.getMessage());
        System.out.println("count:" + qrs.getCount() + " hasMore:" + qrs.isHasMore());
        System.out.println("size:" + qrs.getRecords().size());
        for (Iterator<ColInfo> it = qrs.getMetaData().getColInfo().iterator(); it.hasNext();) {
            ColInfo colInfo = it.next();
            System.out.print(colInfo.getLabel() + "[" + colInfo.getName() + "]\t");
        }
        System.out.println("");
        System.out.println("===============================================================================");
        for (Iterator<LbRecord> it = qrs.getRecords().iterator(); it.hasNext();) {
            LbRecord record = it.next();
            for (Iterator<Object> itVal = record.getValues().iterator(); itVal.hasNext();) {
                System.out.print(itVal.next() + "\t");
            }
            System.out.println("");
        }
    }

    public LoginResult login(LBEBusinessService client) {
        LoginResult result = client.login(userName, passWord, "", "plain", "");

        System.out.println("result message: " + result.getMessage());
        System.out.println("result sessionId: " + result.getSessionId());
        return result;
    }

    /**
     * @return the sessionId
     */
    public String getSessionId() {
        if (sessionId == null) {
            LoginResult result = login(getServiePort());
            sessionId = result.getSessionId();
        }
        return sessionId;
    }

    /**
     * @param sessionId the sessionId to set
     */
    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }



    public void testBizProcess() {
        LBEBusinessService client = getServiePort();
        List<LbParameter> params = new ArrayList<LbParameter>();//入参数组

        LbParameter param = new LbParameter();//入参
        param.setName("USERNAME");//入参名称
        param.setValue("test01");//入参值
        params.add(param);

        param = new LbParameter();
        param.setName("PASSWORD");
        param.setValue("y3Auk2xdAKno/XKq4f76eO9AudmTp9+8ZxLS4HNdy1s=");
        params.add(param);

        param = new LbParameter();
        param.setName("DLLX");
        param.setValue("1");
        params.add(param);

        param = new LbParameter();
        param.setName("ID");
        param.setValue("127.0.0.1");
        params.add(param);

        String objName = "T_GIFAX_USER_YHDL";//要调用的接口对象

        BizProcessResult result = client.execBizProcess(getSessionId(), objName, "", params, null);
        System.out.println("result message: " + result.getMessage());
        System.out.println("result code: " + result.getResult());
        if (result.getOutputVariables() != null) {//接口返回参数
            for (Iterator<LbParameter> it = result.getOutputVariables().iterator(); it.hasNext();) {
                LbParameter var = (LbParameter) it.next();
                System.out.println("out varaible:  " + var.getName() + "=" + var.getValue());
            }
        }

    }
}
